﻿using System;
using log4net;
using log4net.Appender;
using LogLibrary.BusinessEntities;
using LogLibrary.BusinessLogicLayer;
using LogLibrary.DataAccessLayer;
using LogLibrary.Entity;

namespace LogLibrary.Implementation
{
    public class CustomEFAppender : AppenderSkeleton
    {
        public string ConnectionString
        {
            get;
            set;
        }

        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            var logServiceInformation = (LogServiceInformation)GlobalContext.Properties["LogServiceInformation"];
            if (logServiceInformation != null)
            {
                try
                {
                    var logError = new LogData
                    {
                        ProjectId = logServiceInformation.ProjectId,
                        TaskListId = logServiceInformation.TaskListId,
                        CampaignId=logServiceInformation.CampaignId,
                        Type = loggingEvent.Level.Name,
                        Message = loggingEvent.MessageObject == null ? null : Convert.ToString(loggingEvent.MessageObject),
                        OtherInfo = logServiceInformation.OtherInfo == null ? null : Convert.ToString(logServiceInformation.OtherInfo),
                        DateCreated = Convert.ToDateTime(DateTime.Now),
                        StackTrace = logServiceInformation.StackTrace,
                        Exception =
                            logServiceInformation.Exception == null
                                ? logServiceInformation.Message
                                : "Source: " + (logServiceInformation.Exception.Source ?? "") +
                                  "; Message: " + (logServiceInformation.Exception.Message ?? "") +
                                  "; Stack: " + (logServiceInformation.Exception.StackTrace ?? ""),

                    };

                  
                    var db = new DatabaseContext(ConnectionString);
                    new LogDataBLL(db).CreateNewLogData(logError);

                    if (loggingEvent.Level.Name == "ERROR")
                    {
                        string creatationDateTime = "-"+Convert.ToString(logError.DateCreated);
                        string totalInfo = String.Format(Environment.NewLine + "LogType:{0}," + Environment.NewLine + "ProjectId:{1}," + Environment.NewLine + "TaskListId:{2}," + Environment.NewLine + "CampaignId:{3}," + Environment.NewLine + "Exception:{4}", logError.Type + creatationDateTime, logError.ProjectId, logError.TaskListId, logError.CampaignId, logError.Exception);
                        Exception ex = new Exception(totalInfo); 
                        LogManager.GetLogger("LogLibraryErrorLogger").Error(ex);
                    }
                   

                    GlobalContext.Properties["LogServiceInformation"] = null;
                }
                catch (Exception ex)
                {
                    GlobalContext.Properties["OtherInfo"] = logServiceInformation.OtherInfo == null ? " " : Convert.ToString(logServiceInformation.OtherInfo);                    
                    LogManager.GetLogger("LogLibraryErrorLogger").Error(ex);
                    GlobalContext.Properties["LogServiceInformation"] = null;
                }
            }
        }
    }
}
